/**
 * 定义监控数据模块
 */
(function(window, angular, undefined){

	var module = angular.module('water.entity.monitorData',['water.common.dataPage','water.common.AJAXService']);
	
	module.service('$monitorDataService',function($q,$AJAXService,$dataPage){
		
        function getBasePath(){
            return "http://localhost:8090/scy";
        }

		/**
		 * 导出excel
		 */
		this.exportMinitorData = function(queryObj){
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/api/MonitorData/export-minitor-data?"+queryString);
		}

        /**
         * 补传数据
         */
        this.reissueMinitorData = function(queryObj){
            var promise = $AJAXService.post("/MonitorData/reissue-minitor-data",queryObj);
            return promise;
        }

        /**
         * 补生成数据
         */
        this.generateMinitorData = function(queryObj){
            var promise = $AJAXService.post("/MonitorData/generate-minitor-data",queryObj);
            return promise;
        }
		
		/**
		 * 获取当前源数据
		 */
		this.getSourceSingleData = function(){
			var promise = $AJAXService.post("/MonitorData/getSourceSingleData");
			return promise;
		}

        this.getMonitorData = function(queryObj){
            var promise = $AJAXService.post("/MonitorData/getMinitorData",queryObj);
            return promise;
        }
		
		/**
		 * 获取监测数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.monitorDataGetter = function(queryObj,onSuccess,interval){
			
			if(interval == null){
				interval = 2;
			}
			
			var dataGetter = _commonListGetter("/MonitorData/getMinitorData",queryObj,interval,onSuccess);
			return dataGetter;
		}
		
		/**
		 * 通用的数据获取方法
		 * 实时数据，分钟，小时，周期，天公用，报表除外
		 * url:查询的接口地址
		 * queryObj:查询参数
		 * hourPrePage:每一页的小时数
		 * onSuccess:回调
		 */
		function _commonListGetter(url,queryObj,hourPrePage,onSuccess){
			var dataGetter = $dataPage.datetimePage({
				url:url,
				queryObj:queryObj,
				onSuccess:function(data){
					data = _generateLimitInfo(data);
					if(onSuccess){
						onSuccess(data)
					}
				},
				startDateField:'START',
				endDateField:'END',
				hourPrePage:hourPrePage
			});
			return dataGetter;
		}
		
		/**
		 * 处理上下限信息，处理成界面上可以显示的格式
		 * 比如 低限1，高限2，单位mg/l,处理成 1~2[mg/L]
		 * 		低限1,单位mg/l,处理成 >=1[mg/L]
		 * 		高限1,单位mg/l,处理成 <=1[mg/L]
		 * 		无限,单位mg/l,处理成 [mg/L]
		 */
		function _generateLimitInfo(data){
			var pollInfos = data.pollInfo;
			var limitInfos = {};
			for(parm in pollInfos){
				var pollItem = pollInfos[parm];
				var down = pollItem["down"];
				var up = pollItem["up"];
				var unit = pollItem["unit"];
				
				if(!_isEmpty(down) && !_isEmpty(up)){
					pollItem.limitText = down + "~" + up;
				}
				else if(!_isEmpty(down)){
					pollItem.limitText = ">" + down;
				}
				else if(!_isEmpty(up)){
					pollItem.limitText = "<" + up;
				}
				else{
					pollItem.limitText = "";
				}
				pollItem.limitText = pollItem.limitText + "["+unit+"]";
			}
			return data;
		}
		
		function _isEmpty(value){
			if(value == null ||
					value == "" ||
					typeof value === "undefined"){
				return true;
			}
			else{
				return false;
			}
		}		
	})
}(window, angular));